package com.zhijiaoyun.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.dao.IWebsiteLinkDao;
import com.zhijiaoyun.model.WebsiteLink;
import com.zhijiaoyun.model.dto.PageData;
import com.zhijiaoyun.model.dto.PageList;
import com.zhijiaoyun.service.IWebsiteLinkService;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "websiteLinkService")
public class WebsiteLinkServiceImpl implements IWebsiteLinkService {

	@Resource
	IWebsiteLinkDao websiteLinkDao;
	
	@Override
	public WebsiteLink getWebsiteLinkByName(String name) {
		return websiteLinkDao.selectWebsiteLinkByName(name);
	}

	@Override
	public WebsiteLink getWebsiteLinkByLinkUrl(String linkUrl) {
		return websiteLinkDao.selectWebsiteLinkByLinkUrl(linkUrl);
	}

	@Override
	public void addWebsiteLink(WebsiteLink websiteLink) {
		websiteLinkDao.insert(websiteLink);
	}

	@Override
	public WebsiteLink getWebsiteLinkByLinkId(Long linkId) {
		return websiteLinkDao.selectByPrimaryKey(linkId);
	}

	@Override
	public WebsiteLink getWebsiteLinkByNameNotLinkId(Long linkId, String name) {
		return websiteLinkDao.selectWebsiteLinkByNameNotLinkId(linkId, name);
	}

	@Override
	public WebsiteLink getWebsiteLinkByLinkUrlNotLinkId(Long linkId, String linkUrl) {
		return websiteLinkDao.selectWebsiteLinkByLinkUrlNotLinkId(linkId, linkUrl);
	}

	@Override
	public void modifyWebsiteLink(WebsiteLink websiteLink) {
		websiteLinkDao.updateByPrimaryKey(websiteLink);
	}

	@Override
	public PageList<WebsiteLink> pageByParams(Integer pageIndex, String name, String linkUrl) {
		int count = websiteLinkDao.countByParams(name, linkUrl);
		PageData pageData = new PageData();
		List<WebsiteLink> list = new ArrayList<>();
		PageList<WebsiteLink> pageList = new PageList<>();
		if (count > 0) {
			int pageIndexInt = pageIndex == null ? 1 : pageIndex;
			pageData = PageData.createPage(count, pageIndexInt, DsConstant.PAGESIZE_DEFAULT);
			list = websiteLinkDao.pageByParams(pageData.getBeginIndex(), pageData.getPageSize(), name, linkUrl);
			pageList = new PageList<>(list, pageData);
		}
		return pageList;
	}

	@Override
	public void deleteWebsiteLink(WebsiteLink websiteLink) {
		websiteLinkDao.deleteByPrimaryKey(websiteLink.getLinkId());
	}
	
}
